<?php

namespace app\model;

use app\stateData;
use think\facade\Cache;
use think\Model;
use think\model\concern\SoftDelete;

class UserMessage extends Model
{
    /**
     * 消息表
     */
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $createTime='create_date';

    private $PlanTitle = '跟进计划';
    private $PlanType = 1;
    private $InfoTitle = '信息提醒';
    private $InfoType = 7;
    private $CertificateTitle = '证书审核提醒';
    private $CertificateType = 3;

    private $OrderTitle = '订单提醒';
    private $OrderType = 2;

    private $CollectionTitle = '收款审批';
    private $CollectionType = 4;

    private $PayMentTitle = '付款审批';
    private $PayMentType = 5;

    private $PressMoneyTitle = '催款提醒';
    private $PressType = 6;
    private $OrderLogOffTitle = '注销提醒';
    private $OrderLogOffType = 9;

    private $OrderChargebackTitle = '退单提醒';
    private $OrderChargebackType = 8;



    public function addDataModel($data){
        return self::create($data);
    }

    public function getTypeAttr($name): array
    {
        return [
            'id'=>$name,
            'text'=>stateData::$_messageType[$name]
        ];
    }

    public function getUidAttr($name): array
    {
        return [
            'id'=>$name,
            'text'=>(new User)->getIdNickName ($name)?:''
        ];
    }
    public function getFUidAttr($name): array
    {
        return [
            'id'=>$name,
            'text'=>(new User)->getIdNickName ($name)?:''
        ];
    }

    public function getFeedbackReasonDateAttr($name)
    {
        if(empty($name) || $name=='0000-00-00 00:00:00') return "";
        return $name;
    }

    /**
     * 添加跟进计划
     * @param $custom_name //客户名称
     * @param $uid         //接收人员id
     * @param $bid         //绑定业务id
     * @param $execution_date  //执行时间
     * @return \app\model\UserMessage|\think\Model
     */
    public function addPlanModel($custom_name,$uid,$fuid,$bid,$execution_date)
    {
        return self::create([
            'title'=>$this->PlanTitle,
            'content'=>'您有一条关于'.$custom_name.'的跟进计划，请及时处理；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->PlanType,
            'b_id'=>$bid,
            'is_executed'=>2,
            'execution_date'=>$execution_date
        ]);
    }

    /**
     * 添加信息提醒
     * @param $content //信息内容
     * @param $uid     //接收人员id string  多人,分割
     * @param $execution_date //执行时间
     * @return \app\model\UserMessage|\think\Model
     */

    public function addInformationModel($content, $uid,$fuid, $execution_date,$is_executed=1,$b_id='',$comId='')
    {
        return self::create([
            'title'=>$this->InfoTitle,
            'content'=>$content,
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->InfoType,
            'is_executed'=>$is_executed,
            'b_id'=>$b_id,
            'order_company_id'=>$comId,
            'execution_date'=>$execution_date
        ]);
    }

    /**
     * 添加证书审核通过
     * @param $custom_name
     * @param $uid
     * @param $type
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addCertificateExaminePassModel($custom_name, $uid, $fuid,$bid, $execution_date){
        return self::create([
            'title'=>$this->CertificateTitle,
            'content'=>'您有一条关于'.$custom_name.'的证书审核通过，请及时查看；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->CertificateType,
            'b_id'=>$custom_name,
            'b_type'=>1,
            'execution_date'=>$execution_date
        ]);
    }

    /**
     * 添加证书审核驳回
     * @param $custom_name
     * @param $uid
     * @param $type
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addCertificateExamineRefuseModel($custom_name,$uid,$fuid,$bid,$execution_date){
        return self::create([
            'title'=>$this->CertificateTitle,
            'content'=>'您有一条关于'.$custom_name.'的证书审核驳回，请及时处理；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->CertificateType,
            'b_id'=>$custom_name,
            'b_type'=>1,
            'execution_date'=>$execution_date
        ]);
    }

    /**
     * 接收到证书审核，通知相关负责人处理
     * @param $custom_name
     * @param $uid
     * @param $type
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addCertificateExamineModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->CertificateTitle,
            'content'=>'您有一条关于'.$custom_name.'的证书审核，请及时处理；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->CertificateType,
            'b_id'=>$custom_name,
            'b_type'=>1,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 证书办理添加
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addOrderHandleModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->OrderTitle,
            'content'=>'您有一条订单'.$custom_name.'待处理，请及时处理；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->OrderType,
            'b_id'=>$custom_name,
            'b_type'=>4,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 企业证书业务添加
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addOrderBusinessModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->OrderTitle,
            'content'=>'您有一条订单'.$custom_name.'待处理，请及时处理；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->OrderType,
            'b_id'=>$custom_name,
            'b_type'=>5,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 企业倒证添加
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addOrderExchangeModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->OrderTitle,
            'content'=>'您有一条订单'.$custom_name.'待处理，请及时处理；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->OrderType,
            'b_id'=>$custom_name,
            'b_type'=>3,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 匹配证书注册
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $comId
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addOrderCompanyModel($custom_name,$uid,$fuid,$bid,$comId,$execution_date='')
    {
        return self::create([
            'title'=>$this->OrderTitle,
            'content'=>'您有一条订单'.$custom_name.'待处理，请及时处理；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->OrderType,
            'b_id'=>$custom_name,
            'order_company_id'=>$comId,
            'b_type'=>2,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 添加收款审批
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addCollectionModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->CollectionTitle,
            'content'=>'您有一条收款信息'.$custom_name.'待审批，请及时查看；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->CollectionType,
            'b_id'=>$bid,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 收款审批通过
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addCollectionPassModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->CollectionTitle,
            'content'=>'您有一条收款信息'.$custom_name.'已通过，请及时查看；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->CollectionType,
            'b_id'=>$bid,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 收款拒绝
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addCollectionRefuseModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->CollectionTitle,
            'content'=>'您有一条收款信息'.$custom_name.'已拒绝，请及时查看；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->CollectionType,
            'b_id'=>$bid,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 添加付款审批
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addPayMentModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->PayMentTitle,
            'content'=>'您有一条付款信息'.$custom_name.'待审批，请及时查看；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->PayMentType,
            'b_id'=>$bid,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 付款审批通过
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addPayMentPassModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->PayMentTitle,
            'content'=>'您有一条付款信息'.$custom_name.'已通过，请及时查看；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->PayMentType,
            'b_id'=>$bid,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 付款审批拒绝
     * @param $custom_name
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addPayMentRefuseModel($custom_name,$uid,$fuid,$bid,$execution_date='')
    {
        return self::create([
            'title'=>$this->PayMentTitle,
            'content'=>'您有一条付款信息'.$custom_name.'已拒绝，请及时查看；',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->PayMentType,
            'b_id'=>$bid,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }


    /**
     * 催款提醒
     * @param $uid
     * @param $bid
     * @param string $comId
     * @param string $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addPressForMoneyModel($uid, $bid,$fuid, string $comId='', string $execution_date='')
    {
        return self::create([
            'title'=>$this->PressMoneyTitle,
            'content'=>'您有一条催款提醒，请及时处理',
            'uid'=>$uid,
            'f_uid'=>$fuid,
            'type'=>$this->PressType,
            'b_id'=>$bid,
            'order_company_id'=>$comId,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }


    /**
     * 注销
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addLogOffModel($uid,$bid,$fuid,$btype,$oid,$execution_date='')
    {
        return self::create([
            'title'=>$this->OrderLogOffTitle,
            'content'=>'您有一条订单注销提醒，请及时处理',
            'uid'=>$uid,
            'b_type'=>$btype,
            'f_uid'=>$fuid,
            'type'=>$this->OrderLogOffType,
            'b_id'=>$bid,
            'order_company_id'=>$oid,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 退单
     * @param $uid
     * @param $bid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function addChargebackModel($uid,$bid,$fuid,$btype,$oid,$execution_date='')
    {
        return self::create([
            'title'=>$this->OrderChargebackTitle,
            'content'=>'您有一条订单退单提醒，请及时处理',
            'uid'=>$uid,
            'type'=>$this->OrderChargebackType,
            'b_id'=>$bid,
            'f_uid'=>$fuid,
            'order_company_id'=>$oid,
            'b_type'=>$btype,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }

    /**
     * 修改退单业绩并通知相应人员
     * @param $uid
     * @param $bid
     * @param $fuid
     * @param $btype
     * @param $centent
     * @param $oid
     * @param $execution_date
     * @return \app\model\UserMessage|\think\Model
     */
    public function editChargebackModel($uid,$bid,$fuid,$btype,$centent,$oid,$execution_date='')
    {
        return self::create([
            'title'=>$this->OrderChargebackTitle,
            'content'=>$centent,
            'uid'=>$uid,
            'type'=>$this->OrderChargebackType,
            'b_id'=>$bid,
            'f_uid'=>$fuid,
            'order_company_id'=>$oid,
            'b_type'=>$btype,
            'execution_date'=>$execution_date?:date('Y-m-d,H:i:s')
        ]);
    }


    /**
     * 根据分类查询该用户所有消息
     * @param $type
     * @param $uid
     * @return \app\model\UserMessage[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTypeMessageListModel($type,$uid,$page,$pageNum,$delay)
    {
        if (empty($type) && $delay == 1){
            $data = $this->where (['uid'=>$uid])->page ($page,$pageNum)->select();
        }elseif($type && $delay == 2){
            $data = $this->where (['uid'=>$uid,'delay' => $delay])->page ($page,$pageNum)->select();
        }else{
            $data = $this->where (['type'=>$type,'uid'=>$uid,'is_executed'=>1])->page ($page,$pageNum)->select();
        }

        return $data;
    }

    //全部消息
    public function getTypeMessageCountModel($type,$uid,$delay): int
    {

        if (empty($type) && $delay == 1){
            $data = $this->where (['uid'=>$uid])->count();
        }elseif($type && $delay == 2){
            $data = $this->where (['uid'=>$uid,'delay'=>$delay])->count();
        }else{
            $data = $this->where (['type'=>$type,'uid'=>$uid])->count();
        }
        return $data;
    }

    //未读消息
    public function getTypeMessageUnreadCountModel($type,$uid,$delay): int
    {
        if (empty($type)){
            $data = $this->where (['uid'=>$uid,'is_view'=>0])->count();
        }else{
            $data = $this->where (['type'=>$type,'uid'=>$uid,'is_view'=>0])->count();
        }
        return $data;
    }

    /**
     * 全部设置为已读
     * @param $uid
     * @return \app\model\UserMessage
     */

    public function setUidMessageMark($uid)
    {
        return $this->where(['uid'=>$uid])->update (['is_view'=>1]);
    }

    /**
     * 删除信息  id字符串 支持批量删除
     * @param $id
     * @return bool
     */
    public function delMessage($id): bool
    {
        return self::destroy($id);
    }

    /**
     * 根据绑定id删除信息
     * @param $id
     * @return bool
     */
    public function delBidMessageModel($id): bool
    {
        return $this->where (['b_id'=>$id])->delete();
    }

    /**
     * 查询消息详情
     * @param $id
     * @return array|null
     */
    public function getIdFindModel($id): ?array
    {
        $data = $this->where (['id'=>$id])->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 消息延后处理
     * @param $id
     * @param $date
     * @return void
     */
    public function setMessLaterModel($id,$date)
    {
        $this->where (['id'=>$id])->update (['is_executed'=>2,'execution_date'=>$date,'delay'=>2]);
    }

    /**
     * 查询代提醒信息
     * @return \app\model\UserMessage[]|array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWaitSendInfoModel(): array
    {
        return $this->where ('is_executed','=','2')->select()->toArray ();
    }

    /**
     * 修改消息为已发送
     * @param $idArr
     * @return \app\model\UserMessage
     */
    public function editInfoSendModel($idArr)
    {
        return $this->whereIn('id',$idArr)->update (['is_executed'=>1]);
    }


    public function editReadStatus($idArr)
    {
        return $this->whereIn('id',$idArr)->update (['is_read'=>1]);
    }

    /**
     * 添写未收款原因
     * @param $id
     * @param $text
     * @return \app\model\UserMessage
     */
    public function setFeedbackReasonModel($id,$text)
    {
        return $this->where (['id'=>$id])->update (['feedback_reason'=>$text,'feedback_reason_date'=>date('Y-m-d H:i:s')]);
    }

    /**
     * 获取催款原因
     * @param $id
     * @param $cer
     * @return mixed
     */
    public function getPressReason($id,$cer)
    {
        return $this->where (['order_company_id'=>$id,'b_id'=>$cer,'type'=>6])->field('id,create_date,feedback_reason,b_id')->order('create_date DESC')->find ();
    }

    /**
     * 是否有催款原因
     * @param $id
     * @param $cer
     * @return mixed
     */
    public function getPressFindEmpty($id,$cer)
    {
        return $this->where (['order_company_id'=>$id,'b_id'=>$cer,'type'=>6])->value ('id');
    }

    /**
     * 获取催款记录
     * @param $id
     * @param $cer
     * @return mixed
     */
    public function getPressReasonDataModel($id,$cer)
    {
        return $this->where (['order_company_id'=>$id,'b_id'=>$cer,'type'=>6])->field('id,create_date,feedback_reason,b_id,feedback_reason_date')->order('create_date DESC')->select ();
    }

    /**
     * 获取列表定时器内容
     * @param $id
     * @param $cer
     * @return \app\model\UserMessage[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFromRemindModel($id,$cer)
    {
        return $this->where (['order_company_id'=>$id,'b_id'=>$cer,'type'=>7,'is_executed'=>2])->order('create_date DESC')->select ();
    }

    public function getWhereFindModel($execution_date,$uid,$bid)
    {
        $data = $this->where (['uid'=>$uid,'b_id'=>$bid])->order('execution_date ASC')->whereTime('execution_date',$execution_date)->findOrEmpty ();

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 通过id修改内容
     * @param $id
     * @param $data
     * @return \app\model\UserMessage
     */
    public function esitIdFindModel($id,$data)
    {
        return $this->where (['id'=>$id])->update ($data);
    }

    /**
     * 删除计划任务
     */
    public function delWhereFindModel($type,$execution_date,$uid,$bid,$is_executed)
    {
        $data =  $this->where (['uid'=>$uid,'b_id'=>$bid,'is_executed'=>$is_executed])->order('execution_date ASC')->whereTime('execution_date',$execution_date)->findOrEmpty();
        if (!$data->isEmpty ()){
            return self::destroy ($data['id']);
        }
        return false;
    }

    public function delIdFindModel($id)
    {
        return $this->where (['id'=>$id])->destroy($id);
    }

    /**
     * 获取条件相同的数据
     * @param $execution_date
     * @param $uid
     * @param $type
     * @param $bid
     * @param $is_executed
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWhereDataModel($execution_date,$uid,$type,$bid,$is_executed=2)
    {
        return $this->where (['uid'=>$uid,'is_executed'=>$is_executed])->order('execution_date ASC')->whereTime('execution_date',$execution_date)->select()->toArray ();
    }


    public function getNoReadData($uid,$is_executed=1)
    {
        return $this->where('uid',$uid)->where('is_read',0)->where('is_executed',$is_executed)->order('execution_date DESC')->select()->toArray();
    }
}